package com.space.utils;

import com.sun.jmx.snmp.Timestamp;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ToolUtil {
    public static boolean checkValue(String valueStr)
    {
        boolean status = true;
        if (("").equals(valueStr) || ("null").equals(valueStr)
                || ("NULL").equals(valueStr) || valueStr == null
                || ("undefined").equals(valueStr))
        {
            status = false;
        }
        return status;
    }
    public static String checkValueStr(String valueStr)
    {
        if (("").equals(valueStr) || ("null").equals(valueStr)
                || ("NULL").equals(valueStr) || valueStr == null
                || ("undefined").equals(valueStr))
        {
            return "";
        }
        return valueStr;
    }

    public static boolean isNumber(String valStr)
    {
        return valStr.matches("[\\d]+") || valStr.matches("[\\d.]+");
    }
    public static boolean checkIsNum(String valueStr)
    {
        boolean isNum = valueStr.matches("[0-9]+");
        return isNum;
    }
    public static boolean checkStaffNum(String valueStr)
    {
        boolean isNum = valueStr.matches("^[e|E][0-9]+$");
        return isNum;
    }

    public static long checkPhoneNum(String phoneNum)
    {

        Pattern p1 = Pattern.compile("^((\\+{0,1}86){0,1})1[0-9]{10}");
        Matcher m1 = p1.matcher(phoneNum);
        if (m1.matches())
        {
            Pattern p2 = Pattern.compile("^((\\+{0,1}86){0,1})");
            Matcher m2 = p2.matcher(phoneNum);
            StringBuffer sb = new StringBuffer();
            while (m2.find())
            {
                m2.appendReplacement(sb, "");
            }
            m2.appendTail(sb);
            return Long.parseLong(sb.toString());

        }else
        {
            return Long.parseLong(phoneNum);
        }
    }
    public static String printArray(String[] strArray)
    {
        if(strArray!=null)
        {
            StringBuilder strResult=new StringBuilder();
            for(int i=0;i<strArray.length;i++)
            {
                strResult.append(strArray[i]);
                strResult.append(",");
            }
            return strResult.substring(0,strResult.length()-1);
        }else
        {
            return "";
        }
    }
    public static boolean isNull(Object target)
    {
        if(target==null)
        {
            return false;
        }else
        {
            return true;
        }
    }
    public static boolean isPositive(int num)
    {
        if(num>0)
        {
            return true;
        }else
        {
            return false;
        }
    }


    public static Timestamp toTimestampFormat(Date date){
        TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8");
        TimeZone.setDefault(tz);
        if(date!=null){
            return new Timestamp(date.getTime());
        }
        return null;
    }



    public static String getSystemTimeStringFormat(Date date){
        TimeZone tz = TimeZone.getTimeZone("ETC/GMT-8");
        TimeZone.setDefault(tz);
        SimpleDateFormat format   =   new   SimpleDateFormat( "yyyy-MM-dd hh:mm:ss");
        if(date==null){
            date   =   new   Date();
        }
        return format.format(date);
    }

    public static String getCurrentTime() {
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        time.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return time.format(new Date());
    }

    /***
     * MD5加密 生成32位md5码
     * @return 返回32位md5码
     */
    public static String md5Encode(String inStr) throws Exception {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    public static boolean isNotNull(String valueStr)
    {
        boolean status = true;
        if (("").equals(valueStr) || ("null").equals(valueStr)
                || ("NULL").equals(valueStr) || valueStr == null
                || ("undefined").equals(valueStr))
        {
            status = false;
        }
        return status;
    }

    public static String getUUID() {
        return UUID.randomUUID().toString();
    }

    /**
     * 测试主函数
     * @param args
     * @throws Exception
     */
    public static void main(String args[]) throws Exception {
//            String str = new String("admin");
//            System.out.println("原始：" + str);
//            System.out.println("MD5后：" + md5Encode(str));
//            System.out.println("===="+"CN02".substring(0,2));
    }

    public static boolean checkState(String state) {
        boolean flag = false;
        if(state.length()>0 && state.length() <5
                &&state.startsWith("CN")){
            flag = true;
        }
        return flag;
    }

    /**
     * 两数相除
     * @param numerator     分子
     * @param denominator   分母
     * @return
     */
    public static double division(int numerator, int denominator) {
        try{
            DecimalFormat df = new DecimalFormat("0.00");
            return (float)numerator/denominator;
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(e.toString());
            return 0.00;
        }
    }
    public static boolean isValidDate(String str) {
        boolean convertSuccess=true;
        // 指定日期格式为四位年/两位月份/两位日期，注意yyyy/MM/dd区分大小写；
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            // 设置lenient为false. 否则SimpleDateFormat会比较宽松地验证日期，比如2007/02/29会被接受，并转换成2007/03/01
            format.setLenient(false);
            format.parse(str);
        } catch (ParseException e) {
            // e.printStackTrace();
// 如果throw java.text.ParseException或者NullPointerException，就说明格式不对
            convertSuccess=false;
        }
        return convertSuccess;
    }

    public static String errorInfo(Exception e){
        StringWriter stringWriter = new StringWriter();
        e.printStackTrace(new PrintWriter(stringWriter));
        return stringWriter.toString();
    }

}
